package cn.ctyun.videoplayer.controller;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import cn.ctyun.videoplayer.R;
import cn.ctyun.videoplayer.adapter.AdapterMediaQuality;
import cn.ctyun.videoplayer.bean.MediaQualityInfo;
import cn.ctyun.videoplayer.controller.StandardVideoController;
import cn.ctyun.videoplayer.controller.DefinitionMediaPlayerControlInterface;
import cn.ctyun.videoplayer.player.IjkVideoView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * 清晰度切换控制器
 * Created by Devlin_n on 2018/4/16.
 */

public class DefinitionController extends StandardVideoController {
    private TextView multiRate;
    private List<String> mRateStr;

    // 分辨率选择布局
    private View mFlMediaQuality;
    // 清晰度
    private TextView mIvMediaQuality;
    // 分辨率选择列表
    private ListView mLvMediaQuality;
    // 分辨率选择列表适配器
    private AdapterMediaQuality mQualityAdapter;
    // 是否显示分辨率选择列表
    private boolean mIsShowQuality = false;
    // 当前选中的分辨率
    private int mCurSelectQuality = -1;


    // 默认显示/隐藏选择分辨率界面时间
    private static final int DEFAULT_QUALITY_TIME = 300;


    public DefinitionController(@NonNull Context context) {
        this(context, null);
    }

    public DefinitionController(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DefinitionController(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView() {
        super.initView();
        multiRate = mControllerView.findViewById(R.id.tv_multi_rate);
        multiRate.setOnClickListener(this);

        mFlMediaQuality = findViewById(R.id.fl_media_quality);

        mLvMediaQuality = findViewById(R.id.lv_media_quality);
        mQualityAdapter = new AdapterMediaQuality(mAttachActivity);
        mLvMediaQuality.setAdapter(mQualityAdapter);
        mLvMediaQuality.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mCurSelectQuality != position) {
                    mQualityAdapter.setMediaQuality(position);
                    multiRate.setText(mRateStr.get(position));
                    ((DefinitionMediaPlayerControlInterface) mMediaPlayer).switchDefinition(mRateStr.get(position));
                    hide();
                    mCurSelectQuality = position;
                }
                _toggleMediaQuality();
            }
        });

    }

    @Override
    public void setPlayerState(int playerState) {
        super.setPlayerState(playerState);
        switch (playerState) {
            case IjkVideoView.PLAYER_NORMAL:
                multiRate.setVisibility(GONE);
                if (mIsShowQuality) {
                    ViewCompat.animate(mFlMediaQuality).translationX(mFlMediaQuality.getWidth()).setDuration(DEFAULT_QUALITY_TIME);
                    mIsShowQuality = false;
                }
                break;
            case IjkVideoView.PLAYER_FULL_SCREEN:
                multiRate.setVisibility(VISIBLE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.tv_multi_rate) {
            showRateMenu();
        }
    }

    @Override
    public void confirmSingleTap() {
        if (mIsShowQuality) {
            ViewCompat.animate(mFlMediaQuality).translationX(mFlMediaQuality.getWidth()).setDuration(DEFAULT_QUALITY_TIME);
            mIsShowQuality = false;
        } else {
            super.confirmSingleTap();
        }


    }

    private void showRateMenu() {
        if (mFlMediaQuality.getVisibility() == GONE) {
            mFlMediaQuality.setVisibility(VISIBLE);
        }
        if (mIsShowQuality) {
            ViewCompat.animate(mFlMediaQuality).translationX(mFlMediaQuality.getWidth()).setDuration(DEFAULT_QUALITY_TIME);
            mIsShowQuality = false;
        } else {
            ViewCompat.animate(mFlMediaQuality).translationX(0).setDuration(DEFAULT_QUALITY_TIME);
            mIsShowQuality = true;
        }
    }


    @Override
    protected int setProgress() {
        if (multiRate != null && TextUtils.isEmpty(multiRate.getText())) {
            LinkedHashMap<String, String> multiRateData = ((DefinitionMediaPlayerControlInterface) mMediaPlayer).getDefinitionData();
            if (multiRateData == null) {
                return super.setProgress();
            }

            List<MediaQualityInfo> mediaQualityInfos = new ArrayList<>();
            mRateStr = new ArrayList<>();
            int index = 0;
            ListIterator<Map.Entry<String, String>> iterator = new ArrayList<>(multiRateData.entrySet()).listIterator(multiRateData.size());
            while (iterator.hasPrevious()) {//反向遍历
                Map.Entry<String, String> entry = iterator.previous();
                mRateStr.add(entry.getKey());

                mediaQualityInfos.add(new MediaQualityInfo(entry.getKey(), false));
                index++;
            }
            mQualityAdapter.updateItems(mediaQualityInfos);
            mQualityAdapter.setMediaQuality(index - 1);
            multiRate.setText(mRateStr.get(index - 1));
            mCurSelectQuality = index - 1;
        }
        return super.setProgress();
    }

    /**
     * 切换视频分辨率控制
     */
    private void _toggleMediaQuality() {
        if (mFlMediaQuality.getVisibility() == GONE) {
            mFlMediaQuality.setVisibility(VISIBLE);
        }
        if (mIsShowQuality) {
            ViewCompat.animate(mFlMediaQuality).translationX(mFlMediaQuality.getWidth()).setDuration(DEFAULT_QUALITY_TIME);
            mIsShowQuality = false;
        } else {
            ViewCompat.animate(mFlMediaQuality).translationX(0).setDuration(DEFAULT_QUALITY_TIME);
            mIsShowQuality = true;
        }
    }
}
